package xin.yangshuai.basic01.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xin.yangshuai.common01.entity.BaseResult;
import xin.yangshuai.common01.entity.TestEntity;
import xin.yangshuai.common01.entity.User;

import java.util.Date;
import java.util.Map;

/**
 * HelloController
 *
 * @author shuai
 * @date 2022/7/27
 */
@RestController
public class HelloController {

    @Value("${aa.bb}")
    private String info;

    @Autowired
    ObjectMapper objectMapper;

    @RequestMapping("hello")
    public String hello() {
        System.out.println(info);
        System.out.println("hello world");
        return "hello world";
    }

    @RequestMapping("info")
    public User info(HttpServletRequest request) {

        HttpSession session = request.getSession();
        System.out.println(session.getId());

        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                System.out.println(cookie.getName() + "：" + cookie.getValue());
            }
        }

        User user = new User();
        user.setId("001");
        user.setName("张三");
        user.setAge(18);
        user.setBirthday(new Date());

        return user;
    }

    @RequestMapping("infoResult")
    public BaseResult<User> infoResult(HttpServletRequest request) {

        HttpSession session = request.getSession();
        System.out.println(session.getId());

        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                System.out.println(cookie.getName() + "：" + cookie.getValue());
            }
        }

        User user = new User();
        user.setId("001");
        user.setName("张三");
        user.setAge(18);
        user.setBirthday(new Date());

        BaseResult<User> userBaseResult = new BaseResult<>();
        userBaseResult.setCode("0");
        userBaseResult.setMessage("success");
        userBaseResult.setData(user);

        return userBaseResult;
    }

    @RequestMapping("infoResult1")
    public BaseResult<User> infoResult1(HttpServletRequest request) {

        HttpSession session = request.getSession();
        System.out.println(session.getId());

        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                System.out.println(cookie.getName() + "：" + cookie.getValue());
            }
        }

        BaseResult<User> userBaseResult = new BaseResult<>();
        userBaseResult.setCode("0");
        userBaseResult.setMessage("success");

        return userBaseResult;
    }

    @RequestMapping("hi")
    public Object hi(@RequestBody Map map) {
        try {
            System.out.println(objectMapper.writeValueAsString(map));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping("testEntity")
    public TestEntity testEntity(@RequestBody TestEntity testEntity) {
        return testEntity;
    }

}
